package com.lzx.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzx.api.CommonResult;
import com.lzx.entity.Address;
import com.lzx.service.AddressService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Address)表控制层
 *
 * @author makejava
 * @since 2021-02-25 10:39:31
 */
@RestController
@RequestMapping("address")
public class AddressController {

    /**
     * 服务对象
     */
    @Resource
    private AddressService addressService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Address selectOne(Integer id) {
        return this.addressService.queryById(id);
    }

    @RequestMapping("/getUserAddress")
    public CommonResult<List<Address>> getUserAddress(Integer memberId) {
        List<Address> addressList = addressService.list(new QueryWrapper<Address>().eq("userid", memberId));
        if (BeanUtil.isNotEmpty(addressList)) {
            return CommonResult.success(addressList);
        }
        return CommonResult.failed("请先添加收获地址");
    }

    @RequestMapping("/addAddress")
    public CommonResult addAddress(@RequestBody Address address) {
        boolean result = addressService.save(address);
        if (!result) {
            return CommonResult.failed();
        }

        return CommonResult.success(true);
    }

}
